package com.ssy.lingxi.contract.serviceimpl;


import com.ssy.lingxi.contract.entity.SignatureAccount;
import com.ssy.lingxi.contract.entity.SignatureAuth;
import com.ssy.lingxi.contract.entity.SignatureAuthLog;
import com.ssy.lingxi.contract.entity.SignatureOrganization;
import com.ssy.lingxi.contract.esign.api.SignApi;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import com.ssy.lingxi.contract.esign.request.SilentSignSetRequest;
import com.ssy.lingxi.contract.model.bo.sign.SignAuthDetailBO;
import com.ssy.lingxi.contract.model.constant.CommonBooleanEnum;
import com.ssy.lingxi.contract.model.constant.sign.SignatureAuthTypeEnum;
import com.ssy.lingxi.contract.repository.SignatureAccountRepository;
import com.ssy.lingxi.contract.repository.SignatureAuthLogRepository;
import com.ssy.lingxi.contract.repository.SignatureAuthRepository;
import com.ssy.lingxi.contract.repository.SignatureOrganizationRepository;
import com.ssy.lingxi.contract.service.ISignatureUserDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 签章认证用户信息服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/28
 */
@Slf4j
@Service
public class SignatureUserDetailServiceImpl implements ISignatureUserDetailService {

    @Resource
    private SignatureAuthRepository signatureAuthRepository;
    @Resource
    private SignatureAuthLogRepository signatureAuthLogRepository;
    @Resource
    private SignatureAccountRepository signatureAccountRepository;
    @Resource
    private SignatureOrganizationRepository signatureOrganizationRepository;
    @Resource
    private SignApi signApi;

    @Override
    public SignAuthDetailBO getSignAuthDetail(Long memberId) {
        // 签章申请信息
        SignatureAuth signatureAuth = signatureAuthRepository.findByMemberId(memberId);
        if (Objects.isNull(signatureAuth)) {
            return null;
        }

        // 认证信息
        SignatureAuthLog signatureAuthLog = signatureAuthLogRepository.findById(signatureAuth.getSignatureAuthLogId()).orElse(null);
        if (Objects.isNull(signatureAuthLog)) {
            return null;
        }

        // 机构账户信息
        SignatureOrganization signatureOrganization = signatureOrganizationRepository.findByOrgId(signatureAuthLog.getOrgId());

        // 个人账户信息
        SignatureAccount signatureAccount = signatureAccountRepository.findByAccountId(signatureAuthLog.getAccountId());

        SignAuthDetailBO signAuthDetailBO = new SignAuthDetailBO();
        signAuthDetailBO.setMemberId(signatureAuthLog.getMemberId());
        signAuthDetailBO.setAuthType(signatureAuthLog.getAuthType());
        signAuthDetailBO.setVerifyType(signatureAuthLog.getVerifyType());
        signAuthDetailBO.setAccount(signatureAccount);
        signAuthDetailBO.setOrganization(signatureOrganization);
        signAuthDetailBO.setState(signatureAuth.getState());

        return signAuthDetailBO;
    }

    @Override
    public SignatureAuthLog getSignatureAuthLog(Long memberId) {
        // 签章申请信息
        SignatureAuth signatureAuth = signatureAuthRepository.findByMemberId(memberId);
        if (Objects.isNull(signatureAuth)) {
            return null;
        }

        // 认证信息
        SignatureAuthLog signatureAuthLog = signatureAuthLogRepository.findById(signatureAuth.getSignatureAuthLogId()).orElse(null);
        if (Objects.isNull(signatureAuthLog)) {
            return null;
        }

        return signatureAuthLog;
    }

    @Override
    public boolean silentSignAuth(SignAuthDetailBO signAuthDetail) {
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 企业签署静默授权
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (!Objects.isNull(organization)) {
                if (CommonBooleanEnum.NO.getCode().equals(organization.getSilent())) {
                    SilentSignSetRequest silentSignSetRequest = SilentSignSetRequest.builder().accountId(organization.getOrgId()).build();
                    try {
                        signApi.account().silentSignSet(silentSignSetRequest);
                    } catch (EsignException e) {
                        log.error("e签宝企业账号设置默认签署: {}", e.getError());
                        return false;
                    }

                    // 设置成功,更新信息
                    SignatureOrganization saveOrg = signatureOrganizationRepository.findByOrgId(organization.getOrgId());
                    if (Objects.nonNull(saveOrg)) {
                        saveOrg.setSilent(CommonBooleanEnum.YES.getCode());
                        signatureOrganizationRepository.saveAndFlush(saveOrg);
                    }
                }
            }

            // 关联人静默授权
            SignatureAccount account = signAuthDetail.getAccount();
            if (!Objects.isNull(account)) {
                if (CommonBooleanEnum.NO.getCode().equals(account.getSilent())) {
                    SilentSignSetRequest silentSignSetRequest = SilentSignSetRequest.builder().accountId(account.getAccountId()).build();
                    try {
                        signApi.account().silentSignSet(silentSignSetRequest);
                    } catch (EsignException e) {
                        log.error("e签宝企业账号关联人设置默认签署: {}", e.getError());
                        return false;
                    }

                    // 设置成功,更新信息
                    SignatureAccount saveAccount = signatureAccountRepository.findByAccountId(account.getAccountId());
                    if (Objects.nonNull(saveAccount)) {
                        saveAccount.setSilent(CommonBooleanEnum.YES.getCode());
                        signatureAccountRepository.saveAndFlush(saveAccount);
                    }
                }
            }

            return true;
        }

        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人签署静默授权
            SignatureAccount account = signAuthDetail.getAccount();
            if (!Objects.isNull(account)) {
                if (CommonBooleanEnum.NO.getCode().equals(account.getSilent())) {
                    SilentSignSetRequest silentSignSetRequest = SilentSignSetRequest.builder().accountId(account.getAccountId()).build();
                    try {
                        signApi.account().silentSignSet(silentSignSetRequest);
                    } catch (EsignException e) {
                        log.error("e签宝个人账号设置默认签署: {}", e.getError());
                        return false;
                    }

                    // 设置成功,更新信息
                    SignatureAccount saveAccount = signatureAccountRepository.findByAccountId(account.getAccountId());
                    if (Objects.nonNull(saveAccount)) {
                        saveAccount.setSilent(CommonBooleanEnum.YES.getCode());
                        signatureAccountRepository.saveAndFlush(saveAccount);
                    }
                }
                return true;
            }
        }

        return false;
    }
}